Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.fs.IterableDir.Iterator.next(): Do not assume that FILE_BOTH_DIR_INFORMATION is correctly aligned #17958

Merged
merged 2 commits into from
Nov 21, 2023

Conversation

salo-dea
Copy link
Contributor

This PR relaxes the assumed alignment of FILE_BOTH_DIR_INFORMATION in the windows implementation of std.fs.IterabDir.Iterator.next().

I am quite new to zig, so I will happily receive any feedback about how this could be done differently - especially concerning the potential performance penalty of this.

Background

The current windows implementation of std.fs.IterabDir.Iterator.next() makes the assumption that the FILE_BOTH_DIR_INFORMATION struct returned by NtQueryDirectoryFile is aligned properly.

See https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_both_dir_information

This structure must be aligned on a LONGLONG (8-byte) boundary. If a buffer contains two or more of these structures, the NextEntryOffset value in each entry, except the last, falls on an 8-byte boundary.

However, in practice this is not always guaranteed due to faulty file system drivers, sandboxing tools, or (in my case) endpoint security software.
Currently, this code will panic on the @alignCast() which makes it impossible for the user to work around this issue if it appears.

For context, I found that the same issue appeared in Rust:

@salo-dea salo-dea changed the title std.fs.IterabDir.Iterator.next(): not assume that FILE_BOTH_DIR_INFORMATION is correctly aligned std.fs.IterabDir.Iterator.next(): Do not assume that FILE_BOTH_DIR_INFORMATION is correctly aligned Nov 10, 2023
@salo-dea salo-dea changed the title std.fs.IterabDir.Iterator.next(): Do not assume that FILE_BOTH_DIR_INFORMATION is correctly aligned std.fs.IterableDir.Iterator.next(): Do not assume that FILE_BOTH_DIR_INFORMATION is correctly aligned Nov 10, 2023
@Vexu Vexu enabled auto-merge (rebase) November 21, 2023 12:08
@Vexu Vexu merged commit a58ecf7 into ziglang:master Nov 21, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants